<template>
	<view class="container">
		<view class="title">输入安全码</view>

		<!-- 密码显示区域 -->
		<view class="password-display">
			<view v-for="(dot, index) in 4" :key="index" class="password-dot"
				:class="{ filled: index < password.length }"></view>
		</view>

		<!-- 数字键盘 -->
		<view class="keyboard">
			<view class="row" v-for="(row,r_index) in keypad" :key="r_index">
				<view class="key" v-for="(key,k_index) in row" :key="k_index" @tap="onKeyTap(key)">
					{{ key !== 'empty' ? key : '' }}
				</view>
			</view>
		</view>

		<!-- 操作按钮 -->
		<view class="actions">
			<view class="action-btn" @tap="onCancel">取消</view>
			<view class="action-btn" @tap="onDelete">删除</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				password: '', // 当前输入的密码
				keypad: [
					['1', '2', '3'],
					['4', '5', '6'],
					['7', '8', '9'],
					['empty', '0', 'empty']
				]
			};
		},
		methods: {
			// 设置安全码
			getdata() {
				uni.request({
					url: this.$api.baseUrl + '/api/user/safe-code/validate',
					header: {
						'content-type': 'application/json'
					},
					data: {
						user_uid: uni.getStorageSync('user_uid'),
						safe_code: this.password,
					},
					method: 'POST',
					success: (res) => {
						if (res.data.code == 0) {
							uni.navigateBack({
								delta: 1, // 返回的页面层级，1 表示返回上一级页面
							});

						}
						console.log(res)
						this.$util.toast('解锁成功')
					},
					fail: (res) => {
						console.log(res);
					}
				});
			},
			onKeyTap(key) {

				if (key === 'empty' || this.password.length >= 6) return;
				this.password += key;
				if (this.password.length === 4) {
					this.getdata();
				}
			},
			onCancel() {
				this.password = '';
			},
			onDelete() {
				this.password = this.password.slice(0, -1);
			}
		}
	};
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
		height: 100vh;
		padding: 20px;
		background-color: #f5f5f5;
	}

	.title {
		font-size: 18px;
		color: #666;
		margin-top: 30px;
	}

	.password-display {
		display: flex;
		justify-content: center;
		margin: 20px 0;
	}

	.password-dot {
		width: 15px;
		height: 15px;
		margin: 0 10px;
		border: 1px solid #ccc;
		border-radius: 50%;
		background-color: transparent;
	}

	.password-dot.filled {
		background-color: #666;
	}

	.keyboard {
		width: 80%;
	}

	.row {
		display: flex;
		justify-content: space-around;
		margin-bottom: 10px;
	}

	.key {
		width: 60px;
		height: 60px;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 50%;
		background-color: #fff;
		box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
		font-size: 20px;
		color: #666;
	}

	.key:active {
		background-color: #ddd;
	}

	.actions {
		display: flex;
		justify-content: space-between;
		width: 80%;
		margin-top: 20px;
	}

	.action-btn {
		font-size: 16px;
		color: #666;
	}
</style>